/*******************************************************************************
 * FilePath    : /Software/Examples_Ble_MS1658/ble_app_peripheral_LED/main.c
 * Version     : 1.0
 * Brief       : 
 * Author      : AndrewHu
 * Company     : Shanghai MacroGiga Electronics CO.,Ltd
 * Email       : Hubinbo@macrogiga.com
 * Date        : 2021-03-03 17:08:51
 * LastEditors : AndrewHu
 * LastEditTime: 2021-09-27 11:50:24
 * Description : 
 ******************************************************************************/
/*******************************************************************************
*    Copyright (c) 2020 Macrogiga Electronics Co., Ltd.
* 
*    Permission is hereby granted, free of charge, to any person
*    obtaining a copy of this software and associated documentation
*    files (the 'Software'), to deal in the Software without
*    restriction, including without limitation the rights to use, copy,
*    modify, merge, publish, distribute, sublicense, and/or sell copies
*    of the Software, and to permit persons to whom the Software is
*    furnished to do so, subject to the following conditions:
* 
*    The above copyright notice and this permission notice shall be
*    included in all copies or substantial portions of the Software.
* 
*    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
*    DEALINGS IN THE SOFTWARE.
******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <Includes.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define LED_BUFF_MAX 1000
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 ledOnOff = 0x01; //0x00-关
u16 ledBuffR = 0xff;
u16 ledBuffG = 0x00;
u16 ledBuffB = 0x00;
u16 ledBuffC = 0x00;
u16 ledBuffW = 0x00;
/* Private function ----------------------------------------------------------*/
/* extern --------------------------------------------------------------------*/
/*******************************************************************************
 * Function    : Led_SetBuff
 * Brief       : 
 * param        {u16} r
 * param        {u16} g
 * param        {u16} b
 * param        {u16} c
 * param        {u16} w
 * return       {*}
 * Description : 
 * Date        : 2021-09-26 15:23:24
 ******************************************************************************/
void Led_SetBuff(u16 r, u16 g, u16 b, u16 c, u16 w)
{
    ledBuffR = r;
    ledBuffG = g;
    ledBuffB = b;
    ledBuffC = c;
    ledBuffW = w;
}
/*******************************************************************************
 * Function    : ledBleData
 * Brief       : 
 * param        {u8} *data
 * param        {u8} len
 * return       {*}
 * Description : 
 * Date        : 2021-09-26 15:49:05
 ******************************************************************************/
void ledBleData(u8 *data, u8 len)
{
    u8 temp[11] = {0x00};
    switch (data[0])
    {
    case 0xaa: //开关
        if (data[1] == 0x00)
            ledOnOff &= 0xfe;
        else
            ledOnOff |= 0x01;
        break;
    case 0xab: //设置颜色
        ledBuffC = data[3] << 8;
        ledBuffC += data[2];

        ledBuffG = data[5] << 8;
        ledBuffG += data[4];

        ledBuffW = data[7] << 8;
        ledBuffW += data[6];

        ledBuffB = data[9] << 8;
        ledBuffB += data[8];

        ledBuffR = data[11] << 8;
        ledBuffR += data[10];
        break;
    case 0xac: //查询状态
        temp[0] = 0xac;

        temp[1] = ledBuffC >> 8;
        temp[2] = ledBuffC;

        temp[3] = ledBuffG >> 8;
        temp[4] = ledBuffG;

        temp[5] = ledBuffW >> 8;
        temp[6] = ledBuffW;

        temp[7] = ledBuffB >> 8;
        temp[8] = ledBuffB;

        temp[9] = ledBuffR >> 8;
        temp[10] = ledBuffR;

        SendNotifyData(temp, 11);
        break;
    default:
        break;
    }
}
/*******************************************************************************
 * Function    : Key_Run
 * Brief       : 按键信息反馈处理
 * Parameter   : 
 * Returns     : 
 * Date        : 2020-04-04 14:53:26
 * Description : 
 ******************************************************************************/
void Key_Run(void)
{
    static u8 mode = 0x00;
    u8 event = Key_Scan();
    if ((event & KeyFlag_event) == 0x00)
        return;
    Key_ClearFlag();
    event &= 0xf8;
    switch (event)
    {
    case (KeyFlag_event | KeyFlag_times | KeyFlag_down): //按键按下1次
        // case (KeyFlag_event | KeyFlag_times | KeyFlag_down | 0x01): //按键按下1次
        { // Printf_Error(1);
            mode++;
            switch (mode)
            {
            case 0x01:
                ledBuffR = 0x00;
                ledBuffG = 0xff;
                ledBuffB = 0x00;
                break;
            case 0x02:
                ledBuffR = 0x00;
                ledBuffG = 0x00;
                ledBuffB = 0xff;
                break;
            case 0x03:
                ledBuffR = 0xff;
                ledBuffG = 0xff;
                ledBuffB = 0x00;
                break;
            case 0x04:
                ledBuffR = 0x00;
                ledBuffG = 0xff;
                ledBuffB = 0xff;
                break;
            case 0x05:
                ledBuffR = 0xff;
                ledBuffG = 0x00;
                ledBuffB = 0xff;
                break;
            case 0x06:
                ledBuffR = 0xff;
                ledBuffG = 0xff;
                ledBuffB = 0xff;
                break;
            case 0x07:
                ledBuffR = 0x00;
                ledBuffG = 0x00;
                ledBuffB = 0x00;
                ledBuffC = 0xff;
                break;
            case 0x08:
                ledBuffC = 0x00;
                ledBuffW = 0xff;
                break;
            case 0x09:
                ledBuffC = 0xff;
                ledBuffW = 0xff;
                break;
            default:
                mode = 0x00;
                ledBuffC = 0x00;
                ledBuffW = 0x00;
                ledBuffR = 0xff;
                break;
            }
        }
        break;
    case (KeyFlag_event | KeyFlag_times | 0x01): //按键按下1次后"立即"松开
        // Printf_Error(2);
        break;
    case (KeyFlag_event | KeyFlag_times | KeyFlag_down | 0x03): //按键连按3次
        // Printf_Error(3);
        break;
    case (KeyFlag_event | KeyFlag_times | 0x03): //按键连按3次后"立即"松开
        // Printf_Error(4);
        break;
    case (KeyFlag_event | KeyFlag_down | KeyFlag_Long): //按键长按
        // Printf_Error(5);
        if (ledOnOff & 0x02)
            ledOnOff &= (~0x02);
        else
            ledOnOff |= 0x02;
        ledBuffR = LED_BUFF_MAX;
        ledBuffG = LED_BUFF_MAX;
        ledBuffB = LED_BUFF_MAX;
        ledBuffC = 0x00;
        ledBuffW = 0x00;
        break;
    case (KeyFlag_event | KeyFlag_times | KeyFlag_Long): //按键长按后松开
        // Printf_Error(6);
        break;
    case (KeyFlag_event | KeyFlag_times): //按键松开，非连按状态下的松开
        // Printf_Error(7);
        break;
    default:
        // Printf_Error(event);
        break;
    }
}
/*******************************************************************************
 * Function    : main
 * Brief       : 主函数
 * Parameter   : 
 * Returns     : 
 * Date        : 2020-04-03 17:14:47
 * Description : 
 ******************************************************************************/
int main(void)
{
    u32 UnitDelay = 0x2000;
    u8 ledDelay = 0x00;
    u8 ledDelayBuff = 0x00;

    while (UnitDelay--)
        ; //wait a while for hex programming if using the MCU stop mode, default NOT used.

    BSP_Init(); //硬件初始化
    Ble_Init(); //蓝牙初始化
    Uart_Send_String((u8 *)"\r\nIND:Ble Init OK!", 18);
    ble_run_interrupt_start(160 * 2); //广播间隔设定，320*0.625=200 ms
    UnitDelay = GetSysTickCount();    //获取滴答时钟时间，1ms累计
    while (1)
    {
        // IrqMcuGotoSleepAndWakeup(); //蓝牙休眠判定，蓝牙工作方式为间断式工作

        if ((GetSysTickCount() - UnitDelay) >= 5)
        {
            //5ms计时
            UnitDelay = GetSysTickCount();

            //按键扫描
            Key_Run();
            Fun_Uart_Run(); //串口扫描
        }

        ledDelay = GetSysTickCount();
        if (ledDelayBuff != ledDelay)
        { //1ms
            ledDelayBuff = ledDelay;
            if (ledOnOff & 0x01)
            {
                if (ledOnOff & 0x02)
                    setLedStatus(ledBuffR, ledBuffG, ledBuffB, ledBuffC, ledBuffW); //直接变色
                else
                    setLedStatusSlow(ledBuffR, ledBuffG, ledBuffB, ledBuffC, ledBuffW); //渐变
            }
            else
            {
                if (ledOnOff & 0x02)
                    setLedStatus(0, 0, 0, 0, 0); //直接变色
                else
                    setLedStatusSlow(0, 0, 0, 0, 0); //渐变
            }
        }
    }
}
